package com.pedro.encoder.input.gl.render.filters;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.pedro.encoder.R;
import com.pedro.encoder.utils.gl.GlUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class BoxBorderFilterRender extends BaseFilterRender {

  private final float[] squareVertexDataFilter = {
      -1f, -1f, 0f, 0f, 0f,
       1f, -1f, 0f, 1f, 0f,
      -1f,  1f, 0f, 0f, 1f,
       1f,  1f, 0f, 1f, 1f,
  };

  private int program = -1;
  private int aPositionHandle = -1;
  private int aTextureHandle = -1;
  private int uMVPMatrixHandle = -1;
  private int uSTMatrixHandle = -1;
  private int uSamplerHandle = -1;
  private int uCenterHandle = -1;
  private int uSizeHandle = -1;
  private int uBorderHandle = -1;
  private int uColorHandle = -1;
  private int uResolutionHandle = -1;

  private float xCenter = 0.5f;
  private float yCenter = 0.5f;
  private float boxWidth = 0.3f;
  private float boxHeight = 0.3f;
  private float border = 0.005f; // 默认 0.5% 宽
  private float[] color = {1f, 0f, 0f, 1f}; // 红色

  public BoxBorderFilterRender() {
    squareVertex = ByteBuffer.allocateDirect(squareVertexDataFilter.length * FLOAT_SIZE_BYTES)
        .order(ByteOrder.nativeOrder())
        .asFloatBuffer();
    squareVertex.put(squareVertexDataFilter).position(0);
    Matrix.setIdentityM(MVPMatrix, 0);
    Matrix.setIdentityM(STMatrix, 0);
  }

  @Override
  protected void initGlFilter(Context context) {
    String vertexShader = GlUtil.getStringFromRaw(context, R.raw.simple_vertex);
    String fragmentShader = GlUtil.getStringFromRaw(context, R.raw.box_border_fragment);

    program = GlUtil.createProgram(vertexShader, fragmentShader);
    aPositionHandle = GLES20.glGetAttribLocation(program, "aPosition");
    aTextureHandle = GLES20.glGetAttribLocation(program, "aTextureCoord");
    uMVPMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");
    uSTMatrixHandle = GLES20.glGetUniformLocation(program, "uSTMatrix");
    uSamplerHandle = GLES20.glGetUniformLocation(program, "uSampler");
    uCenterHandle = GLES20.glGetUniformLocation(program, "uCenter");
    uSizeHandle = GLES20.glGetUniformLocation(program, "uSize");
    uBorderHandle = GLES20.glGetUniformLocation(program, "uBorder");
    uColorHandle = GLES20.glGetUniformLocation(program, "uColor");
    uResolutionHandle = GLES20.glGetUniformLocation(program, "uResolution");
  }

  @Override
  protected void drawFilter() {
    GLES20.glUseProgram(program);

    squareVertex.position(SQUARE_VERTEX_DATA_POS_OFFSET);
    GLES20.glVertexAttribPointer(aPositionHandle, 3, GLES20.GL_FLOAT, false,
        SQUARE_VERTEX_DATA_STRIDE_BYTES, squareVertex);
    GLES20.glEnableVertexAttribArray(aPositionHandle);

    squareVertex.position(SQUARE_VERTEX_DATA_UV_OFFSET);
    GLES20.glVertexAttribPointer(aTextureHandle, 2, GLES20.GL_FLOAT, false,
        SQUARE_VERTEX_DATA_STRIDE_BYTES, squareVertex);
    GLES20.glEnableVertexAttribArray(aTextureHandle);

    GLES20.glUniformMatrix4fv(uMVPMatrixHandle, 1, false, MVPMatrix, 0);
    GLES20.glUniformMatrix4fv(uSTMatrixHandle, 1, false, STMatrix, 0);

    GLES20.glUniform2f(uCenterHandle, xCenter, yCenter);
    GLES20.glUniform2f(uSizeHandle, boxWidth, boxHeight);
    GLES20.glUniform1f(uBorderHandle, border);
    GLES20.glUniform4fv(uColorHandle, 1, color, 0);
    GLES20.glUniform2f(uResolutionHandle, getWidth(), getHeight());

    GLES20.glUniform1i(uSamplerHandle, 4);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE4);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, previousTexId);
  }

  @Override
  public void release() {
    GLES20.glDeleteProgram(program);
  }

  /** 设置中心 (0-100 %) */
  public void setCenter(float x, float y) {
    xCenter = x / 100f;
    yCenter = y / 100f;
  }

  /** 设置方框大小 (0-100 %) */
  public void setBoxSize(float width, float height) {
    this.boxWidth = width / 100f;
    this.boxHeight = height / 100f;
  }

  /** 设置边框厚度 (0-5 %) */
  public void setBorder(float percent) {
    this.border = percent / 100f;
  }

  /** 设置颜色 (0~1) */
  public void setColor(float r, float g, float b, float a) {
    this.color[0] = r;
    this.color[1] = g;
    this.color[2] = b;
    this.color[3] = a;
  }
}
